﻿using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace Listener_SMS
{
    class HqHttpClient
    {

        public enum MethodType
        {
            Post, Get
        }

        private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true; //总是接受
        }

        public HttpWebResponse get(string url) {
            return WebServiceApp(url, MethodType.Get,null);
        }

        public HttpWebResponse post(string url,string param)
        {
            return WebServiceApp(url, MethodType.Post, param);
        }

        public HttpWebResponse WebServiceApp(string url, MethodType method, string param){
            try
            {
                //创建一个HTTP请求  
                HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;

                //2． 初始化HttpWebRequest对象
                webRequest.UserAgent = "com.yunq.listenersms";
                webRequest.Proxy = null;
                webRequest.ContentType = "application/x-www-form-urlencoded";
                if (MethodType.Post.Equals(method))
                {
                    webRequest.Method = "post";
                }
                else {
                    webRequest.Method = "get";
                }
                
                if (url.StartsWith("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                    webRequest.ProtocolVersion = System.Net.HttpVersion.Version10;

                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
                }

                if (!string.IsNullOrEmpty(param)) {
                    //转换输入参数的编码类型，获取bytep[]数组 
                    byte[] byteArray = Encoding.UTF8.GetBytes(param);
                    //3． 附加要POST给服务器的数据到HttpWebRequest对象(附加POST数据的过程比较特殊，它并没有提供一个属性给用户存取，需要写入HttpWebRequest对象提供的一个stream里面。)
                    Stream newStream = webRequest.GetRequestStream();//创建一个Stream,赋值是写入HttpWebRequest对象提供的一个stream里面
                    newStream.Write(byteArray, 0, byteArray.Length);
                    newStream.Close();
                }

                //获取返回json数据
                HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
                return response;
            }
            catch (WebException ex)
            {

            }
            return null;
        }

        public string responseToString(HttpWebResponse response) {
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                return reader.ReadToEnd();
            }
            finally {
                reader.Close();
                response.Close();
            }
        }
    }
}
